Skip to content

Avoid panics when parsing nil slice and map pointers - #307

Merged
alexflint merged 2 commits into
alexflint:masterfrom
yuefdev:fix/nil-collection-pointer-sol
Oct 19, 2026
Merged

alexflint merged 2 commits into
alexflint:masterfrom
yuefdev:fix/nil-collection-pointer-sol

Conversation

@yuefdev

@yuefdev yuefdev commented Oct 18, 2026

Copy link
Copy Markdown
Contributor

NewParser() accepts fields such as *[]string and *map[string]int, but parsing
a value into a nil one panics with reflect: call of reflect.Value.Type on zero Value. The same happens with a CSV environment variable.

var args struct {
    Values *[]string
}
p, _ := arg.NewParser(arg.Config{}, &args)
p.Parse([]string{"--values", "a", "b"}) // panics before this change

Allocate the pointed-to collection before dereferencing it in setSliceOrMap().
The four public-API regressions cover slices/maps from flags/environment
variables. They all panic before the fix; they now parse correctly and also
check that omitted fields remain nil.

Full go test ./... -count=1, go vet ./..., gofmt and git diff --check pass
on Windows/Go 1.27.

@alexflint alexflint left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yuefdev Thanks for this. Happy to accept this change but can you put these tests into parser_test.go and follow more closely the structure of other tests in that file? No need to use a for loop and t.Run in these tests, for example -- please just have 4 separate tests for the 4 cases you're testing.

@yuefdev

yuefdev commented Oct 18, 2026

Copy link
Copy Markdown
Contributor Author

Cheers, Alex! I've moved the cases into parse_test.go as four separate tests, using the existing helpers and style. All four regression tests pass.

@alexflint
alexflint merged commit 5428f86 into alexflint:master Oct 19, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants